package com.filetransfer.view.Components;
import com.filetransfer.utils.SvgIconUtil;
import com.formdev.flatlaf.extras.FlatSVGIcon;

import javax.swing.*;
import java.awt.*;
public class SideMenu extends JPanel {
    private final JPanel buttonContainer;
    private final JPanel bottomContainer;
    private final CardLayout cardLayout;
    private final JPanel mainPanel;
    private boolean isSideMenuExpanded = false;
    public SideMenu(CardLayout cardLayout, JPanel mainPanel) {
        this.cardLayout = cardLayout;
        this.mainPanel = mainPanel;
        setPreferredSize(new Dimension(60, 0));
        setLayout(new BorderLayout());
        setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, UIManager.getColor("Separator.foreground")));
        // Create top button container
        buttonContainer = new JPanel();
        buttonContainer.setLayout(new BoxLayout(buttonContainer, BoxLayout.Y_AXIS));
        add(buttonContainer, BorderLayout.NORTH);
        // Create bottom button container
        bottomContainer = new JPanel();
        bottomContainer.setLayout(new BoxLayout(bottomContainer, BoxLayout.Y_AXIS));
        bottomContainer.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
        add(bottomContainer, BorderLayout.SOUTH);
        // Add toggle button
        FlatSVGIcon menuIcon = SvgIconUtil.createSvgIcon("icons/menu.svg", 20, 20);
        JButton toggleButton = createMenuButton(menuIcon, "");
        toggleButton.addActionListener(e -> toggleSideMenu());
        buttonContainer.add(toggleButton);
    }
    public void addMenuButton(String cardName, String buttonLabel, String iconPath) {
        FlatSVGIcon buttonIcon = SvgIconUtil.createSvgIcon(iconPath, 20, 20);
        JButton button = createMenuButton(buttonIcon, buttonLabel);
        button.setActionCommand(buttonLabel);
        button.addActionListener(e -> cardLayout.show(mainPanel, cardName));
        buttonContainer.add(button);
    }
    public void addBottomButton(String cardName, String buttonLabel, String iconPath) {
        FlatSVGIcon buttonIcon = SvgIconUtil.createSvgIcon(iconPath, 20, 20);
        JButton button = createMenuButton(buttonIcon, buttonLabel);
        button.setActionCommand(buttonLabel);
        button.addActionListener(e -> cardLayout.show(mainPanel, cardName));
        bottomContainer.add(button);
    }
    private void toggleSideMenu() {
        Timer timer = new Timer(10, null);
        final int step = 5;
        final int collapsedWidth = 60;
        final int expandedWidth = 150;
        boolean targetExpanded = !isSideMenuExpanded;
        timer.addActionListener(e -> {
            Dimension currentSize = getPreferredSize();
            int currentWidth = currentSize.width;
            if (targetExpanded && currentWidth < expandedWidth) {
                setPreferredSize(new Dimension(currentWidth + step, getHeight()));
            } else if (!targetExpanded && currentWidth > collapsedWidth) {
                setPreferredSize(new Dimension(currentWidth - step, getHeight()));
            } else {
                timer.stop();
                isSideMenuExpanded = targetExpanded;
                for (Component component : buttonContainer.getComponents()) {
                    if (component instanceof JButton) {
                        JButton button = (JButton) component;
                        button.setText(isSideMenuExpanded ? button.getActionCommand() : "");
                    }
                }
                for (Component component : bottomContainer.getComponents()) {
                    if (component instanceof JButton) {
                        JButton button = (JButton) component;
                        button.setText(isSideMenuExpanded ? button.getActionCommand() : "");
                    }
                }
            }
            revalidate();
            repaint();
        });
        timer.start();
    }
    private JButton createMenuButton(FlatSVGIcon icon, String text) {
        JButton button = new JButton(isSideMenuExpanded ? text : "", icon);
        button.setFocusPainted(false);
        button.setBorderPainted(false);
        button.setContentAreaFilled(false);
        button.setHorizontalAlignment(SwingConstants.LEFT);
        button.setHorizontalTextPosition(SwingConstants.RIGHT);
        button.setMargin(new Insets(5, 17, 5, 0));
        return button;
    }
}
